import {
    request
} from '../../utils/api'
import {
    sexcolumns,
    politicscolumns,
    relationshipcolumns,
    careercolumns,
    conditioncolumns,
    gxcolumns
} from '../../utils/columus.js'
Page({
    data: {
        user: null,
        userId: 0,
        membersList: [],
        score: null,
        question: null,
    },

    editInfo() {
        wx.navigateTo({
            url: `/pages/personal/info/info?id=${this.data.userId}&flag=true`,
        })
    },
    editMamber(){
        wx.navigateTo({
            url: `/pages/details/family/family?id=${this.data.userId}&flag=true`,
        })
    },
    editScore(){
        wx.navigateTo({
            url: `/pages/details/score/score?id=${this.data.userId}&flag=true`,
        })
    },
    editquestion(){
        wx.navigateTo({
            url: `/pages/details/question/question?id=${this.data.userId}&flag=true`,
        })
    },

    async getInfo() {
        let result = await request({
            url: '/user/getUserInfoById?userId=' + this.data.userId,
            method: 'get'
        })
        if (result.result) {
            let user = result.result
            this.setData({
                user
            })

            let politicalOutlook = politicscolumns.filter(item => {
                if (item.id == user.politicalOutlook) {
                    return item
                }
            })
            this.setData({
                ['user.sexName']: user.sex == 1 ? '男' : '女',
                ['user.politicalOutlookName']: politicalOutlook[0].text
            })
            this.setData({
                ['user.fileUrl']: 'https://img.yzcdn.cn/vant/cat.jpeg',
                ['user.photoUrl']: 'https://img.yzcdn.cn/vant/cat.jpeg',
                fileList: [{
                    url: 'https://img.yzcdn.cn/vant/cat.jpeg'
                }],
                fileList1: [{
                    url: 'https://img.yzcdn.cn/vant/cat.jpeg'
                }]
            })
        }
    },

    async getMembers() {
        let res = await request({
            url: '/user/getFamilyMembers?userId=' + this.data.userId,
            method: 'get',
        })
        if (res.result[0].membersList.length > 0) {
            let membersList = res.result[0].membersList
            membersList.forEach((item) => {
                relationshipcolumns.forEach(relationship => {
                    if (item.relationship == relationship.id) {
                        item.relationshipName = relationship.text
                    }
                })
                careercolumns.forEach(career => {
                    if (item.career == career.id) {
                        item.careerName = career.text
                    }
                })
                conditioncolumns.forEach(condition => {
                    if (item.condition == condition.id) {
                        item.conditionName = condition.text
                    }
                })
            })
            this.setData({
                membersList
            })
        } else {
            this.setData({
                membersList: []
            })
        }
    },
    async getScore() {
        let res = await request({
            url: '/user/getScore?userId=' + this.data.userId,
            method: 'get'
        })
        this.setData({
            score: JSON.parse(res.result.scoreJson)
        })
        console.log(this.data.score);
    },
    async getQuestion() {
        let res = await request({
            url: '/user/getQuestion?userId=' + this.data.userId,
            method: 'get'
        })
        gxcolumns.forEach(item => {
            if (res.result.relationship == item.id) {
                res.result.relationshipName = item.text
            }
        })
        console.log(res.result);
        this.setData({
            question: res.result
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            userId: options.userId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo()
        this.getMembers()
        this.getScore()
        this.getQuestion()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})